package com.web.biz.brandweb.service;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Map;

import com.css.sword.kernel.base.annotation.Service;
import com.css.sword.kernel.base.annotation.ServiceContainer;
import com.css.sword.kernel.base.persistence.IPersistenceService;
import com.css.sword.kernel.base.persistence.SQLParam;
import com.css.sword.kernel.utils.SwordLogUtils;
import com.css.sword.kernel.utils.SwordPersistenceUtils;
import com.css.sword.kernel.utils.SwordServiceUtils;
import com.css.sword.platform.web.comm.WebRootUtil;
import com.css.sword.platform.web.event.IReqData;
import com.css.sword.platform.web.event.IResData;
import com.css.sword.platform.web.event.SwordRes;
import com.web.common.dataDictionary.DE.DE_CATEGORY_ID;
import com.web.common.dataDictionary.DE.DE_CATEGORY_OPPTIME;
import com.web.common.dataDictionary.DE.DE_CATEGORY_SFNEW;
import com.web.common.dataDictionary.DE.DE_C_BRAND_EXTTIME;
import com.web.common.dataDictionary.DE.DE_C_BRAND_FILEDOMAIN;
import com.web.common.dataDictionary.DE.DE_C_BRAND_FILENEWPATH;
import com.web.common.dataDictionary.DE.DE_C_BRAND_ID;
import com.web.common.dataDictionary.DE.DE_C_BRAND_NEWNUM;
import com.web.common.dataDictionary.DE.DE_C_BRAND_OPPTIME;
import com.web.common.dataDictionary.DE.DE_C_BRAND_SFNEW;
import com.web.common.dataDictionary.DE.DE_C_BRAND_WEALTH;
import com.web.common.dataDictionary.DE.DE_C_BRAND_WEALTH_GQSJ;
import com.web.common.dataDictionary.DE.DE_C_BRAND_WEALTH_SJXZ;
import com.web.common.dataDictionary.DE.DE_WEBSHSJ;
import com.web.common.dataDictionary.DE.DE_WEBSHZT;
import com.web.common.dataDictionary.PO.TCategoryPO;
import com.web.common.dataDictionary.PO.TCategoryShopPO;
import com.web.common.utils.DateFormatUtil;
import com.web.common.utils.LoginInfoUtil;
import com.web.common.utils.StringUtil;

@ServiceContainer
public class BrandWebService {

	private static SwordLogUtils log = SwordLogUtils.getLogger(BrandWebService.class);
	private final static String curpath = "/pages/biz/brand/";
	public BrandWebService() throws Exception {
		super();
	}


	/**
	 * 初始化品牌表单页面
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked" })
	@Service(serviceName = "BrandWebService_initWebForm")
	public IResData initWebForm(IReqData req) throws Exception {

		log.debug("开始进入 BrandWebService_initWebForm 方法。");

		IResData res = new SwordRes();
		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();
		String yhm=LoginInfoUtil.getLoginUserName();
		
		//往前台返回一些数据用来页面作判断开始
		//用户信息
		Map<String, Object> userMap = (Map<String, Object>) SwordServiceUtils.callLocalService("LoginRegisterService_queryByYhm", yhm);
		res.addJSTL("vipsyts", StringUtil.parse2String(userMap.get("vipsyts")));
		res.addJSTL("shzt", StringUtil.parse2String(userMap.get("shzt")));
		//店铺信息
		SQLParam param = new SQLParam();
		param.addParam(yhm);
		TCategoryShopPO shopPO = dao.queryPOByKey("grxx_queryShopxxByYhm", param, TCategoryShopPO.class);
		//商家财富值不足时"用户点击增加财富"设置为0，时间过期后也设置为0
		if (shopPO.getCBrandTotalwealth().value < shopPO.getCBrandWealth().value) {
			shopPO.setCBrandWealth(new DE_C_BRAND_WEALTH(0));
			shopPO.setCBrandWealthSjxz(new DE_C_BRAND_WEALTH_SJXZ(0));
		}
		String gqsj = DateFormatUtil.date2str(shopPO.getCBrandWealthGqsj().value,DateFormatUtil.DATE_FORMAT);
		Timestamp gqsjTimeStamp = new Timestamp(DateFormatUtil.getTimeByDateStr(gqsj, DateFormatUtil.DATE_FORMAT));
		Timestamp todayTimeStamp = new Timestamp(DateFormatUtil.getTimeByDateStr(DateFormatUtil.getNowDate(), DateFormatUtil.DATE_FORMAT));
		if (todayTimeStamp.after(gqsjTimeStamp)) {
			shopPO.setCBrandWealth(new DE_C_BRAND_WEALTH(0));
			shopPO.setCBrandWealthSjxz(new DE_C_BRAND_WEALTH_SJXZ(0));
		}
		res.addJSTL("brandwebshzt", shopPO.getWebshzt().value);
		res.addJSTL("newnum", shopPO.getCBrandNewnum().value);
		res.addJSTL("totalwealth", shopPO.getCBrandTotalwealth().value);
		res.addJSTL("cBrandFilenewpath", shopPO.getCBrandFilenewpath().value);
		res.addJSTL("cBrandFileoldpath", shopPO.getCBrandFileoldpath().value);
		//往前台返回一些数据用来页面作判断结束
		
		res.addForm("form_brandsz", shopPO.toMap());
		res.addForm("form_brandweb", shopPO.toMap());
		res.addPage(curpath+"brandWebForm.jsp");
		return res;

	}

	 		
	/**
	 * 用户提交web页
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@Service(serviceName = "BrandWebService_tjWeb")
	public IResData tjWeb(IReqData req) throws Exception {
		
		log.debug("开始进入 BrandWebService_tjWeb 方法。");
		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();

		IResData res = new SwordRes();
		res = (IResData) SwordServiceUtils.callLocalService("FreeMarkerService_genHtmlAndGetPath", req);
		String tjflag="false";
		try {
			if("true".equals(StringUtil.parse2String(res.get("flag")))){
				String htmlPath = StringUtil.parse2String(res.get("htmlPath"));
				TCategoryShopPO po=(TCategoryShopPO) req.getForm("form_brandweb",TCategoryShopPO.class);
				po.setCBrandFilenewpath(new DE_C_BRAND_FILENEWPATH(htmlPath));
				po.setCBrandFiledomain(new DE_C_BRAND_FILEDOMAIN(WebRootUtil.getWebRootPath()));
				po.setWebshzt(new DE_WEBSHZT("dsh"));
				po.setWebshsj(new DE_WEBSHSJ(DateFormatUtil.getNowDate()));
				int i = dao.updatePO(po);
				if (i >= 1) {
					tjflag = "true";
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		if ("true".equals(tjflag)) {
			IResData webFormRes = (IResData) SwordServiceUtils.callLocalService("BrandWebService_initWebForm", req);
			res.joinRes(webFormRes);
		}
		res.addAttr("tjflag", tjflag);
		return res;
	}
	
	
	/**
	 * 用户更新设置new
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes" })
	@Service(serviceName = "BrandWebService_updateBrandSz")
	public IResData updateBrandSz(IReqData req) throws Exception {	
		
		log.debug("开始进入 BrandWebService_updateBrandSz 方法。");
		
		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();
		String szflag = "false";
		IResData res = new SwordRes();
		//店铺信息
		Map shopMap =(Map) SwordServiceUtils.callLocalService("LoginRegisterService_queryShopxxByYhm", LoginInfoUtil.getLoginUserName());
		//TCategoryShopPO po=(TCategoryShopPO) req.getForm("form_brandsz",TCategoryShopPO.class);
		Map<String, String> szmap = req.getForm("form_brandsz");
		String cBrandId = szmap.get("cBrandId");
		String categoryId = szmap.get("categoryId");
		String cBrandSfnew = szmap.get("cBrandSfnew");
		TCategoryShopPO po = new TCategoryShopPO();
		po.setCBrandId(new DE_C_BRAND_ID(cBrandId));
		po.setCBrandExttime(new DE_C_BRAND_EXTTIME(DateFormatUtil.getNowDate()));
		if ("0".equals(StringUtil.parse2String(shopMap.get("cBrandSfnew"))) && "1".equals(cBrandSfnew)) {
			po.setCBrandOpptime(new DE_C_BRAND_OPPTIME(DateFormatUtil.getNowDate()));
			po.setCBrandSfnew(new DE_C_BRAND_SFNEW("1"));
			po.setCBrandNewnum(new DE_C_BRAND_NEWNUM(Integer.valueOf(StringUtil.parse2String(shopMap.get("cBrandNewnum")))-1));
			//修改分类的时间
			TCategoryPO categoryPO = new TCategoryPO();
			categoryPO.setCategoryId(new DE_CATEGORY_ID(categoryId));
			categoryPO.setCategorySfnew(new DE_CATEGORY_SFNEW("1"));
			categoryPO.setCategoryOpptime(new DE_CATEGORY_OPPTIME(DateFormatUtil.getNowDate()));
			dao.updatePO(categoryPO);
		}
		int i = dao.updatePO(po);
		if (i >= 1) {
			szflag = "true";
		}
		/*if ("true".equals(szflag)) {
			IResData webFormRes = (IResData) SwordServiceUtils.callLocalService("BrandWebService_initWebForm", req);
			res.joinRes(webFormRes);
		}*/
		res.addAttr("szflag", szflag);
		return res;
		
	}
	


	

	/**
	 * 用户更新设置wealth
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@Service(serviceName = "BrandWebService_updateBrandSzWealth")
	public IResData updateBrandSzWealth(IReqData req) throws Exception {

		log.debug("开始进入 BrandWebService_updateBrandSzWealth 方法。");

		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();
		String szflag = "false";
		IResData res = new SwordRes();
		Map<String, String> szmap = req.getForm("form_brandsz");
		String cBrandId = szmap.get("cBrandId");
		int cBrandWealth = Integer.parseInt(szmap.get("cBrandWealth"));
		int cBrandWealthSjxz = Integer.parseInt(szmap.get("cBrandWealthSjxz"));
		TCategoryShopPO po = new TCategoryShopPO();
		po.setCBrandId(new DE_C_BRAND_ID(cBrandId));
		po.setCBrandExttime(new DE_C_BRAND_EXTTIME(DateFormatUtil.getNowDate()));
		po.setCBrandWealthSjxz(new DE_C_BRAND_WEALTH_SJXZ(cBrandWealthSjxz));
		po.setCBrandWealth(new DE_C_BRAND_WEALTH(cBrandWealth));
		//设置用户增加财富值的过期时间
		po.setCBrandWealthGqsj(new DE_C_BRAND_WEALTH_GQSJ(DateFormatUtil.addDay(new Date(), cBrandWealthSjxz)));
		int i = dao.updatePO(po);
		if (i >= 1) {
			szflag = "true";
		}
		res.addAttr("szflag", szflag);
		return res;
	}

}